package com.example.pongv1;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;

import org.andengine.entity.primitive.Line;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.color.Color;
import org.andengine.util.math.MathUtils;



import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.example.pongv1.BaseScene;
import com.example.pongv1.Joueur;
import com.example.pongv1.SceneManager.SceneType;
import com.example.pongv1.IntelligenceArtificielle;

public class PongScene extends BaseScene implements Constantes, IOnSceneTouchListener{

	private HUD gameHUD;
	private Text scoreText;

public PhysicsWorld physicsWorld;



//***************************************************************************
//Implementation sans passer par le RessourcesManager
//***************************************************************************
//***************************************************************
	//Balle
	private Balle balle;
	//Position balle : 
	//Joueurs
	private Joueur joueur1;
	private Joueur joueur2;
	//Lignes de but
	private Line but_gauche;
	private Line but_droite;
	private Murs murhaut;
	private Murs murbas;
	//***************************************************************


	//***************************************************************
	// Fields
	//***************************************************************

	
	Body leftBody;
	Body rightBody;
	//***************************************************************
	//IA******************
	private IntelligenceArtificielle IA;

	
	//*****************************************************************
	//**************GETTER - SETTER************************************
	//*****************************************************************

	





	
	@Override
	public void createScene() {
		// TODO Auto-generated method stub
		createPhysics();
		createBackground();
		createHUD();
		initscene();
		init_score();
		
		init_partie();
		this.setOnSceneTouchListener(this);
		boucle_jeu();
	}

	@Override
	public void onBackKeyPressed() {
		// TODO Auto-generated method stub
	
		SceneManager.getInstance().loadMenuScene();
		if(ressourcesManager.sound_activated)
		{
			ressourcesManager.music.play();
		}
		else{
			ressourcesManager.music.pause();
		}
		
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.SCENE_GAME;
	}

	@Override
	public void disposeScene() {
		// TODO Auto-generated method stub
		RessourcesManager.getInstance().unloadPongSceneTextures();
		camera.setHUD(null);
		
		
	}
	private void createBackground()
	{
	    //setBackground(new Background(Color.BLACK));
		attachChild(new Sprite(-CAMERA_WIDTH/2, -CAMERA_HEIGHT/2, ressourcesManager.scene_background_region, vbom));
		
	}

	private void createHUD()
	{
		 gameHUD = new HUD();
		    
		    // CREATE SCORE TEXT
		    scoreText = new Text(CAMERA_WIDTH/2, 50, ressourcesManager.font, "Score: 0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
		    //scoreText.setAnchorCenter(0, 0);
		    
		    
		    scoreText.setText("Score: 0");
		    gameHUD.attachChild(scoreText);	    
		    camera.setHUD(gameHUD);
		    
	}
	
	
	
	
	private void createPhysics()
	{
	    
	    
	    this.physicsWorld = new FixedStepPhysicsWorld(30, 2, new Vector2(0,0), false, 8, 8);
	    registerUpdateHandler(physicsWorld);
	

	}



private void initscene(){
	
			//Balle
			balle = new Balle(this);
			attachChild(balle.get_balle());
			//attachChild(balle.ball);
			//Raquette joueur 1
			
			joueur1=new Joueur(this,0,ressourcesManager.IAlevel,ressourcesManager.playername);
			attachChild(joueur1.get_raquette());
			
			joueur2=new Joueur(this,1,ressourcesManager.IAlevel,"IA1");
			attachChild(joueur2.get_raquette());
			
			murhaut=new Murs(this, -CAMERA_WIDTH/2, -CAMERA_HEIGHT/2, CAMERA_WIDTH/2, -CAMERA_HEIGHT/2,2,ressourcesManager.vbom);
			murbas=new Murs(this,-CAMERA_WIDTH/2, CAMERA_HEIGHT/2, CAMERA_WIDTH/2, CAMERA_HEIGHT/2,2,ressourcesManager.vbom);
			
			//Dessin d'une ligne au milieu
			attachChild(new Line(0,CAMERA_HEIGHT/2,0,-CAMERA_HEIGHT/2,2,ressourcesManager.vbom));
			//Lignes de but
			but_gauche = new Line(-CAMERA_WIDTH/2+1,CAMERA_HEIGHT,-CAMERA_WIDTH/2+1,-CAMERA_HEIGHT,2,ressourcesManager.vbom);
			but_droite = new Line(CAMERA_WIDTH/2-1,CAMERA_HEIGHT,CAMERA_WIDTH/2-1,-CAMERA_HEIGHT,2,ressourcesManager.vbom);

		attachChild(but_gauche);
		attachChild(but_droite);
	
			IA=new IntelligenceArtificielle(joueur2.get_level());
	
}

public void score_point(Joueur joueur){
	int newscore=joueur.get_score()+1;
	joueur.set_score(newscore);
	
	
	//condition de fin possible numero1
	if(newscore==10)
	{
		fin_partie(joueur);
	
	}
}


private void fin_partie(Joueur joueur) {
	TimerHandler time;
	//TODO: ajout sprite fin de partie
	//TODO:dire player gagne plus score. plus gain XP player.
	ressourcesManager.engine.registerUpdateHandler(time=new TimerHandler(2f,new ITimerCallback() {
		
		@Override
		public void onTimePassed(TimerHandler pTimerHandler) {
			
			init_score();
			if(ressourcesManager.sound_activated){
			ressourcesManager.game_overSound.play();
			}
		}
	}));
	
	
	
}
public void init_score(){
	joueur1.set_score(0);
	joueur2.set_score(0);
	
}


public void init_partie(){
	balle.init_position();
	
}

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
			
			joueur1.deplacement(pSceneTouchEvent);
	return false;
}


public void boucle_jeu(){
	this.registerUpdateHandler(new TimerHandler(1 / 20.0f, true, new ITimerCallback()
	{
		@Override
		public void onTimePassed(final TimerHandler pTimerHandler)
		{
			scoreText.setText(Integer.toString(joueur1.get_score())+ "   "+ Integer.toString(joueur2.get_score()));
			//Recentrer le score
			scoreText.setPosition(CAMERA_WIDTH/2-scoreText.getWidth()/2, scoreText.getHeight()/2);

			balle.deplacement();
			
			executeIA();
			execute_calcul();

		}
	}));
	
}


public void execute_calcul(){

	if ( but_gauche.collidesWith(balle.get_balle())) {
		if(ressourcesManager.sound_activated){
		ressourcesManager.butSound.play();
		}
		score_point(joueur2);
		init_partie();
	}
	if ( but_droite.collidesWith(balle.get_balle())) {
		if(ressourcesManager.sound_activated){
		ressourcesManager.butSound.play();
		}
		score_point(joueur1);
		init_partie();
	}
	if ( murhaut.collidesWith(balle.get_balle())) {
		if(ressourcesManager.sound_activated){
		ressourcesManager.collisionSound.play();
		}
		
	}
	if ( murbas.collidesWith(balle.get_balle())) {
		if(ressourcesManager.sound_activated){
		ressourcesManager.collisionSound.play();
		}
		
	}
	if ( joueur1.platform.collidesWith(balle.get_balle())) {
		if(ressourcesManager.sound_activated){
		ressourcesManager.racketSound.play();
		}
	}
	if ( joueur2.platform.collidesWith(balle.get_balle())) {
		if(ressourcesManager.sound_activated){
		ressourcesManager.racketSound.play();
		}
	}
	
}


public void executeIA(){
	
	IA.executeIA(balle,joueur2);
	
}




}